package com.hjli.tuling;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import com.alibaba.fastjson.JSONObject;
import com.hjli.tool.PropertiesTool;

public class TuRingTool {

	private static String SECRET;
	private static String APIKEY;

	String PROPERTY_NAME = "tuling.properties";

	public TuRingTool() {
		SECRET = PropertiesTool.getValueByKey(PROPERTY_NAME, "secret");
		APIKEY = PropertiesTool.getValueByKey(PROPERTY_NAME, "apikey");
	}

	public static String sendMsgToTuRing(String msg) {
		// 待加密的json数据
		String data = "{\"key\":\"" + APIKEY + "\",\"info\":\"" + msg + "\"}";
		// 获取时间戳
		String timestamp = String.valueOf(System.currentTimeMillis());

		// 生成密钥
		String keyParam = SECRET + timestamp + APIKEY;
		String key = TuRingMD5.MD5(keyParam);

		// 加密
		TuRingAES mc = new TuRingAES(key);
		data = mc.encrypt(data);

		// 封装请求参数
		JSONObject json = new JSONObject();
		json.put("key", APIKEY);
		json.put("timestamp", timestamp);
		json.put("data", data);
		
		// 请求图灵api
		return PostServer.SendPost(json.toString(), "http://www.tuling123.com/openapi/api");
	}
	
	public static String noSecretSend(String message) throws IOException{
		//申请图灵机器人后获取  
        String APIKEY = "957ea38d15ae46abb6e511ee142e456d";  
        String question = message;// 这是上传给云机器人的问题  
        String INFO = URLEncoder.encode(question, "utf-8");  
        String getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY+ "&info=" + INFO;  
        URL getUrl = new URL(getURL);  
        HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();  
        connection.connect();  
        // 取得输入流，并使用Reader读取  
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));  
        StringBuffer sb = new StringBuffer();  
        String line = "";  
        while ((line = reader.readLine()) != null) {  
            sb.append(line);  
        }  
        reader.close();  
        // 断开连接  
        connection.disconnect();  
        return sb.toString();
	}
	 public static void main(String[] args) throws IOException {  
        //申请图灵机器人后获取  
        String APIKEY = "957ea38d15ae46abb6e511ee142e456d";  
        String question = "你好？";// 这是上传给云机器人的问题  
        String INFO = URLEncoder.encode(question, "utf-8");  
        String getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY+ "&info=" + INFO;  
        URL getUrl = new URL(getURL);  
        HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();  
        connection.connect();  
        // 取得输入流，并使用Reader读取  
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));  
        StringBuffer sb = new StringBuffer();  
        String line = "";  
        while ((line = reader.readLine()) != null) {  
            sb.append(line);  
        }  
        reader.close();  
        // 断开连接  
        connection.disconnect();  
        System.out.println(sb);//{"code":100000,"text":"嘿嘿，你好我好，大家都好~"}  
    }  
}
